//
//  LstActionViewController.m
//  Four_Fit_New
//
//  Created by xalo on 16/7/18.
//  Copyright © 2016年 xmaz. All rights reserved.
//

#import "LstActionViewController.h"
#import <AVFoundation/AVFoundation.h>

#import "LstAction.h"
#import "TrainModel.h"

@interface LstActionViewController ()
{
    NSInteger currentIndex;
    
}
@property(nonatomic,strong)AVPlayer *player;
@property(nonatomic,strong)AVPlayerLayer *playerLayer;

@property(nonatomic,strong)KRVideoPlayerController *KRVideoVC;

@property (weak, nonatomic) IBOutlet UILabel *name;
@property (weak, nonatomic) IBOutlet UILabel *levelPart;
@property (weak, nonatomic) IBOutlet UILabel *action;
@property (weak, nonatomic) IBOutlet UILabel *essentials;
@property (weak, nonatomic) IBOutlet UILabel *coachPrompt;
@property (weak, nonatomic) IBOutlet UILabel *prompt;

@property (weak, nonatomic) IBOutlet UIButton *lastAction;
@property (weak, nonatomic) IBOutlet UILabel *currentActionNumber;
@property (weak, nonatomic) IBOutlet UILabel *AllActionNumber;
@property (weak, nonatomic) IBOutlet UIButton *nextAction;

@property(nonatomic,strong)NSMutableArray *useDataSource;
@property (weak, nonatomic) IBOutlet UILabel *lastLabel;
@property (weak, nonatomic) IBOutlet UILabel *nextLabel;
@property (weak, nonatomic) IBOutlet UIView *lastV;
@property (weak, nonatomic) IBOutlet UIView *nextV;
@property (nonatomic,strong) CAShapeLayer *lastShape;
@property (nonatomic,strong) CAShapeLayer *nextShape;

@property(nonatomic,strong) UIButton *backbn;

@end

@implementation LstActionViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    currentIndex = -1;
    self.view.backgroundColor = [UIColor clearColor];
    LstAction *model = [self.dataSource[self.indexPath.section] lstArray][self.indexPath.row];
    [self setSomethingWithModel:model];
}
- (void)setSomethingWithModel:(LstAction *)model{
    self.name.text = model.name;
    NSString *str = nil;
    if ([model.difficultyLevel isEqualToString:@"0"]) {
        str = @"初级";
    }else if ([model.difficultyLevel isEqualToString:@"1"]){
        str = @"中级";
    }else{
        str = @"高级";
    }
    self.levelPart.text = [NSString stringWithFormat:@"%@ %@",str,model.part];
    self.essentials.text = model.essentials;
    if ([self.essentials.text isEqualToString:@" "]) {
        self.action.text = nil;
    }else{
        self.action.text = @"动作要领:";
    }
    self.prompt.text = model.prompt;
    if ([self.prompt.text isEqualToString:@" "]) {
        self.coachPrompt.text = nil;
    }else{
        self.coachPrompt.text = @"教练提示:";
    }
    
    //视频
    [self playVideoWithUrl:[NSURL URLWithString:model.actionVideoURL]];
    //anniu
    [self.KRVideoVC.view addSubview:self.backbn];
    for (int i = 0; i < self.useDataSource.count; i++) {
        if ([self.useDataSource[i] isEqual:model]) {
            currentIndex = i;
        }
    }
    self.AllActionNumber.text = [NSString stringWithFormat:@"/%ld",(long)self.useDataSource.count];
    self.currentActionNumber.text = [NSString stringWithFormat:@"%ld",(long)currentIndex + 1];
    if(currentIndex == 0){
        self.lastLabel.hidden = YES;
        self.lastV.hidden = YES;
        self.nextLabel.text = [self.useDataSource[currentIndex + 1] name];
    }else if(currentIndex == self.useDataSource.count - 1){
        self.nextLabel.hidden = YES;
        self.nextV.hidden = YES;
        self.lastLabel.text = [self.useDataSource[currentIndex - 1] name];
    }else{
        self.lastLabel.hidden = NO;
        self.nextLabel.hidden = NO;
        self.lastV.hidden = NO;
        self.nextV.hidden = NO;
        self.lastLabel.text = [self.useDataSource[currentIndex - 1] name];
        self.nextLabel.text = [self.useDataSource[currentIndex + 1] name];
    }
    //左边三角
    [self.lastV.layer addSublayer:self.lastShape];
    //右边
    [self.nextV.layer addSublayer:self.nextShape];
    
    
}
//播放视频方法
- (void)playVideoWithUrl:(NSURL *)url{
    if (!self.KRVideoVC) {
        //frame
        self.KRVideoVC = [[KRVideoPlayerController alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.width * (9.0 / 16.0))];
        //让暂停全屏等按钮隐藏
        self.KRVideoVC.view.subviews.lastObject.hidden = YES;
        //循环播放
        self.KRVideoVC.repeatMode = MPMovieRepeatModeOne;
        __weak typeof(self)weakSelf = self;
        //调用视频消失方法时 ...
        [self.KRVideoVC setDimissCompleteBlock:^{
            weakSelf.KRVideoVC = nil;
        }];
        //因为本页面有上移动画 让播放页面0.5S后显示
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.KRVideoVC showInWindow];
        });
    }
    self.KRVideoVC.contentURL = url;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)backAction:(UIButton *)sender {
    [self.KRVideoVC dismiss];
    [UIView animateWithDuration:0.5 animations:^{
        self.view.frame = CGRectMake(0, kHeight, kWidth, kHeight);
    } completion:^(BOOL finished) {
        [self.view removeFromSuperview];
        [self removeFromParentViewController];
    }];
}
- (IBAction)lastAction:(UIButton *)sender {
    if (currentIndex == 0) {
        return;
    }

    currentIndex--;
    [self setSomethingWithModel:self.useDataSource[currentIndex]];
}
- (IBAction)nextAction:(UIButton *)sender {
    if (currentIndex == self.useDataSource.count - 1) {
        return;
    }

    currentIndex++;
    [self setSomethingWithModel:self.useDataSource[currentIndex]];
}

#pragma 属性懒加载
- (NSMutableArray *)useDataSource{
    if (!_useDataSource) {
        _useDataSource = [[NSMutableArray alloc] init];
        for (TrainModel *model in self.dataSource) {
            for (LstAction *md in model.lstArray) {
                [_useDataSource addObject:md];
            }
        }
    }
    return _useDataSource;
}

- (UIButton *)backbn{
    if (!_backbn) {
        _backbn = [UIButton buttonWithType:UIButtonTypeCustom];
        _backbn.frame = CGRectMake(15, 25, 15, 15);
        [_backbn addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
        CAShapeLayer *shape = [CAShapeLayer layer];
        UIBezierPath *path = [[UIBezierPath alloc] init];
        [path moveToPoint:CGPointMake(0, 0)];
        [path addLineToPoint:CGPointMake(_backbn.width / 2.0, _backbn.height / 3 * 1.7)];
        [path addLineToPoint:CGPointMake(_backbn.width, 0)];
        shape.path = path.CGPath;
        shape.fillColor = [UIColor clearColor].CGColor;
        shape.strokeColor = [UIColor whiteColor].CGColor;
        shape.lineWidth = 1.5;
        [_backbn.layer addSublayer:shape];
    }
    return _backbn;
}
- (CAShapeLayer *)lastShape{
    if (!_lastShape) {
        _lastShape = [CAShapeLayer layer];
        UIBezierPath *lastBezier = [[UIBezierPath alloc] init];
        [self.lastV layoutIfNeeded];
        [lastBezier moveToPoint:CGPointMake(self.lastV.width, 0)];
        [lastBezier addLineToPoint:CGPointMake(self.lastV.width, self.lastV.height)];
        [lastBezier addLineToPoint:CGPointMake(3, self.lastV.height / 2.0)];
        _lastShape.path = lastBezier.CGPath;
        _lastShape.fillColor = [UIColor whiteColor].CGColor;
    }
    return _lastShape;
}
- (CAShapeLayer *)nextShape{
    if (!_nextShape) {
        _nextShape = [CAShapeLayer layer];
        UIBezierPath *nextBezier = [[UIBezierPath alloc] init];
        [self.nextV layoutIfNeeded];
        [nextBezier moveToPoint:CGPointMake(0, 0)];
        [nextBezier addLineToPoint:CGPointMake(self.nextV.width - 3, self.nextV.height / 2.0)];
        [nextBezier addLineToPoint:CGPointMake(0, self.nextV.height)];
        _nextShape.path = nextBezier.CGPath;
        _nextShape.fillColor = [UIColor whiteColor].CGColor;
    }
    return _nextShape;
}
- (AVPlayer *)player{
    if (!_player) {
        _player = [[AVPlayer alloc] init];
    }
    return _player;
}
@end
